--[[
Author: wangxiaoyan
Date: 2020-09-27 15:07:06
LastEditTime: 2020-12-02 20:58:22
FilePath: \Lua\View\UIBase.lua

UI基类
1. 为UI提供更加明确的流程控制
    OnCreate    gameObject创建成功后触发
    OnEnable    显示时触发
    OnDisable   隐藏时触发
    OnDestroy   销毁时触发
2. 对象清理
    注册的事件和消息会在UI卸载时自动清理
3. 提供更加方便的方法
4. 提供UIComponent功能 方便编写UI组件
--]]

require 'View/UIEvent'

---@class UIBase : UIComponentInterface
---@field New fun():UIBase
UIBase = Class("UIBase", UIComponentInterface)

local _M = UIBase

function _M:ctor(panelName)
    self._panelName = panelName
    rawset(self,"_eventList",{})
end

-- 创建（gameobject创建之后）
function _M:Create(obj)
    ---@type GComponent
    self.fguiObj = obj
    self:OnCreate()
end


-- 显示
function _M:Enable()
    if self == nil then
        return
    end
    self:OnEnable()
    self:OnComEnable()
end

-- 隐藏
function _M:Disable()
    self:OnComDisable()
    self:OnDisable()
end


-- 卸载
function _M:Destroy()
    self:UnRegisterEvent()
    self.fguiObj = nil
    self:OnComDestroy()
    self:OnDestroy()
end



-- 注册Event
function _M:RegisterEvent(key,onCall,handle)
    if self._eventList == nil then
        return
    end
    if onCall == nil then
        logError("RegisterEvent callback is nil")
        return
    end
    if self._eventList[key] then
        logError("RegisterEvent 重复的key "..key)
        CloseEvent(key,self._eventList[key])
    end
    local handle = handle or self
    local betterOnCall = function(...)
        --if isOpenUI(self._panelName) then
            if handle == nil then
                onCall(...)
            else
                onCall(handle,...)
            end
        --end
    end
    ListenEvent(key,betterOnCall)
    self._eventList[key] = betterOnCall
end

function _M:RemoveEvent(key)

    local c = self._eventList[key]
    if not c then return end
    CloseEvent(key,c)
end
-- 卸载Event
function _M:UnRegisterEvent()
    if self._eventList == nil then
        return
    end

    for key, value in pairs(self._eventList) do
        CloseEvent(key,value)
    end

    table.clear(self._eventList)
end

--增加一个组件(luaCom继承UIComponentBase)
---@param obj GComponent
---@param luaComTemplate UIComponentBase
function _M:AddComponent(luaComTemplate,obj,...)
    if not self._components then
        rawset(self,"_components",{})
    end
    local tCom = luaComTemplate.New(obj,self)
    tCom:Init(...)
    tCom:Create()
    tCom:Enable()
    table.insert(self._components,tCom)
    return tCom
end
---@param obj GComponent
---@return UIComponentBase
function _M:GetComponentByObj(obj)
    if self._components == nil then
        return
    end
    for k,v in pairs(self._components) do
        if v.fguiObj == obj then
            return v
        end
    end
    return nil
end
--删除一个组件
function _M:RemoveComponent(com)
    if self._components == nil then
        return
    end
    for k,v in pairs(self._components) do
        if (v == com) then
            com:Disable()
            com:Destroy()
           table.remove(self._components,k)
            break
        end
    end
end
function _M:OnComDestroy()
    if self._components == nil then
        return
    end
    for key, value in pairs(self._components) do
        value:Destroy()
    end
    table.clear(self._components)
end
function _M:OnComEnable()
    if self._components == nil then
        return
    end
    for key, value in pairs(self._components) do
        value:Enable()
    end
end
function _M:OnComDisable()
    if self._components == nil then
        return
    end
    for key, value in pairs(self._components) do
        value:Disable()
    end
end
--region override
function _M:Init(...)

end
function _M:OnCreate()end
function _M:OnDestroy()
end
function _M:OnEnable()

end
function _M:OnDisable()
end
--endregion override